var currentGid;

function modifyStaff(obj) {
    $("#create").fadeOut();
    $("#warning").slideDown(500);
    $.getJSON("getstaff", {
        gid: obj.value
    }, function (result) {
        console.log(result);
        currentGid = obj.value;
        $("#gname").val(result["obj"]["Name"]); 
        $("#gprice").val(result["obj"]["Phone"]);
    })
}

//修改
$(function () {
    var modal = $("#warning");
    $("#miniclose").click(function () {
        modal.fadeOut()
    });
    $("#gdelete").click(function () {
        $.getJSON("deletestaff", {
            gid: currentGid
        }, function (result) {
            console.log(result);
            console.log(result["err"]);
            if (result["err"] === null) {
                alert("删除成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    });
    $("#gcancle").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure").click(function () {
        var data = {
            id: currentGid,
            name: $("#gname")[0].value,
            phone: $("#gprice")[0].value,
            active:true,
            motion: "update",
        };
        $.getJSON("updatestaff", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("修改成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    })
});

//新建
$(function () {
    modal = $("#create");
    $("#createstaff").click(function () {
        $("#warning").fadeOut(500);
        modal.slideDown()
    });

    $("#gcancle2").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure2").click(function () {
        var data = {
            name: $("#gname2")[0].value,
            phone: $("#gprice2")[0].value,
            active:true,
            motion: "create",
            state:1
        };
        $.getJSON("updatestaff", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("增加成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
                $("#gname2")[0].val("");
                $("#gprice2")[0].val("");
            }
        })
    });
    $("#miniclose2").click(function () {
        modal.fadeOut()
    })
});

//离职,请假
function holiday(obj) {
    var state;
    if (obj.title == 2){
        state = 1
    }else{
        state = 2
    }
    var data = {
        id: obj.value,
        state: state,
        active:true,
        motion: "update",
    }
    $.getJSON("updatestaff", data, function (result) {
        console.log(result);
        if (result["err"] === null && result["row"] === 1) {
            alert("变更成功!");
            window.location.reload()
        } else {

        }
    })
}

function reverse(obj) {
    var data = {
        id: obj.value,
        active: true,
        state:1,
        motion: "update",
    }
    $.getJSON("updatestaff", data, function (result) {
        console.log(result);
        if (result["err"] === null && result["row"] === 1) {
            alert("变更成功!");
            window.location.reload()
        } else {

        }
    })
}

function quit(obj) {
    var data = {
        id: obj.value,
        active: false,
        motion: "update",
    }
    $.getJSON("updatestaff", data, function (result) {
        console.log(result);
        if (result["err"] === null && result["row"] === 1) {
            alert("变更成功!");
            window.location.reload()
        } else {

        }
    })
}

function deletestaff(obj) {
    $.getJSON("deletestaff", {
        gid: obj.value
    }, function (result) {
        console.log(result);
        console.log(result["err"]);
        if (result["err"] === null) {
            alert("删除成功!");
            window.location.reload()
        } else {
            modal.fadeOut(500);
        }
    })
}